//
//  CXHInterstitialViewController.m
//  CXHAdSDK_Example
//
//  Created by 陈坤 on 2022/3/24.
//  Copyright © 2022 陈桑葚吃桑葚. All rights reserved.
//

#import "CXHInterstitialViewController.h"
#import <CXHAdSDK-Core/CXHAdSDK.h>
#import <CXHAdSDK-Core/CXHAdSDKInterstitialAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>
#import <CXHAdSDK-Core/CXHAdSDKNativeInterstitialAd.h>
#import <CXHAdSDK-Core/CXHAdSDKNativeAd.h>
#import <CXHAdSDK-Adapter/CXHAdSDKAdapter.h>
#import <CXHAdSDK-Kit/UIImageView+CXHWebImage.h>

@interface CXHInterstitialViewController ()<CXHAdSDKInterstitialAdDelegate,CXHAdSDKNativeInterstitialAdDelegate,CXHAdSDKNativeAdDelegate>{
    CXHAdSDKInterstitialAd *_interstitialAd;
    UIButton *_loadButton;
    CXHAdSDKNativeInterstitialAd *_interAd;
    CXHAdSDKNativeAd *_nativeAd;
}

@property (nonatomic) bool isAdLoaded;

@end

@implementation CXHInterstitialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _isAdLoaded = NO;
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem *itemLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onCancelClicked:)];
    self.navigationItem.leftBarButtonItem = itemLeft;
    
    UIButton *btn2 = [UIButton new];
    btn2.backgroundColor = [UIColor orangeColor];
    btn2.layer.cornerRadius = 20;
    
    btn2.frame = CGRectMake((kCXHScreenWidth -150)/2, kCXHTopBarHeight + 100, 150, 40);
    [btn2 setTitle:@"加载广告" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _loadButton = btn2;
    [self.view addSubview:btn2];
    [btn2 addTarget:self action:@selector(loadNativeAd) forControlEvents:UIControlEventTouchUpInside];
}

- (void)onCancelClicked:(id)sender {
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
}

- (void)test2 {
    if (_isAdLoaded) {
        [self showAd];
        return;
    }
    [self loadAd];
}

- (void)loadNativeInterstitialAd{
    CXHAdSDKNativeInterstitialAd *interAd = [[CXHAdSDKNativeInterstitialAd alloc]init];
    _interAd = interAd;
    _interAd.delegate = self;
    _interAd.controller = self;
    // 设置插屏广告的展示时长为5s，5s后自动消失
//    _interAd.showTime = 5;
//    [_interAd setAdScenes:@{
//        @"station_id":@"无锡",
//        @"in_out":@"1",
//        @"userid":@"123",
//    }];
    [_interAd loadAd:@"55301062"];
}

- (void)loadNativeAd{
    CXHAdSDKNativeAd *nativeAd  = [[CXHAdSDKNativeAd alloc]init];
    _nativeAd = nativeAd;
    _nativeAd.delegate = self;
    _nativeAd.controller = self;
//    _nativeAd.posId = @"22047521";
    _nativeAd.posId = @"Screen_Insertion";
    [_nativeAd load:1];
    
}

#pragma mark - CXHAdSDKNativeAdDelegate
/**
 信息流原生广告请求成功

 @param nativeAd 广告实例
 @param adViewArray 广告模板集合
 */
- (void)CXH_nativeAdSucessToLoad:(CXHAdSDKNativeAd *)nativeAd
                     adViewArray:(NSArray<__kindof UIView<CXHAdapterNativeAdViewDelegate> *> *)adViewArray{
    UIView<CXHAdapterNativeAdViewDelegate> *adview = [adViewArray firstObject];
    [adview cxh_registViews:@[adview]];
    CGFloat width = [UIScreen mainScreen].bounds.size.width - 16 * 2;
    CGFloat height = width * 9 / 16;
    adview.frame = CGRectMake(16, ([UIScreen mainScreen].bounds.size.height - height)/2, width, height);
    [self.view addSubview:adview];
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.frame = CGRectMake(0, 0, width, height);
    [adview addSubview:imageView];
    CXHAdapterNativeAdData *adData = [adview cxh_nativeAdData];
    [imageView cxh_setImageWithURL:[NSURL URLWithString:adData.imageUrl] options:0];
    
    UILabel *adLabel = [[UILabel alloc]init];
    adLabel.text = @"广告";
    adLabel.frame = CGRectMake(adview.frame.size.width - 25, adview.frame.size.height - 15, 25, 15);
    adLabel.textAlignment = NSTextAlignmentCenter;
    adLabel.font = [UIFont systemFontOfSize:10];
    [adview addSubview:adLabel];
    adLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0  blue:153/255.0 alpha:1];
    adLabel.backgroundColor = [UIColor colorWithHexString:@"#666666"];
}

/**
 信息流广告请求失败

 @param nativeAd 广告实例
 @param error 请求错误描述
 */
- (void)CXH_nativeAdFailToLoad:(CXHAdSDKNativeAd *)nativeAd
                         error:(NSError *)error{
    NSLog(@"err:%@", error);
}

/**
 信息流广告被点击

 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClicked:(CXHAdSDKNativeAd *)nativeAd
                     adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView loadingPageURL:(NSString *)loadingPageURL{
    
}

/**
 信息流广告被关闭
 
 @param nativeAd 广告模板
 */
- (void)CXH_nativeAdClose:(CXHAdSDKNativeAd *)nativeAd
                   adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告被展示
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdExposure:(CXHAdSDKNativeAd *)nativeAd
                      adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

/**
 信息流广告落地页被关闭
 
 @param nativeAd 广告实例
 */
- (void)CXH_nativeAdCloseLandingPage:(CXHAdSDKNativeAd *)nativeAd
                              adView:(__kindof UIView<CXHAdapterNativeAdViewDelegate> *)adView{
    
}

#pragma mark - CXHAdSDKNativeInterstitialAdDelegate
- (void)nativeInterstitialAdSuccessToLoad:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    [_interAd showAdView];
}

- (void)nativeInterstitialAdFailedToLoad:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    
}

- (void)nativeInterstitialAdExposure:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    
}

- (void)nativeInterstitialAdClick:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    
}

- (void)nativeInterstitialAdClose:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    
}

- (void)nativeInterstitialAdLoadingPageViewClose:(CXHAdSDKNativeInterstitialAd *)nativeInterstitialAd{
    
}

- (void)loadAd{
    _isAdLoaded = YES;
    [_loadButton setTitle:@"展示广告" forState:UIControlStateNormal];
    _interstitialAd = [[CXHAdSDKInterstitialAd alloc]init];
    _interstitialAd.delegate = self;
    _interstitialAd.controller = self;
    _interstitialAd.posId = @"Screen_Insertion";
    // 场景id
    [_interstitialAd setInterstitialScenes:@{
        @"station_id":@"无锡",
        @"in_out":@"1",
        @"userid":@"123",
    }];
    
    [_interstitialAd loadAdData];
}

- (void)showAd {
    if (!_isAdLoaded) {
        return;
    }
    if (![_interstitialAd isAdValid]) {
        return;
    }
    [_interstitialAd show];
}

#pragma mark - CXHAdSDKInterstitialAdDelegate

/**
 CXHAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdSuccedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd{
//    // 推荐在该回调之后展示插屏广告
    dispatch_async(dispatch_get_main_queue(), ^{
        [self showAd];
    });
}

/**
 CXHAdSDKIntertitialAd请求失败回调

 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
*/
- (void)CXH_interstitialAdFailedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    _interstitialAd = nil;
    _isAdLoaded = NO;
    [_loadButton setTitle:@"加载广告" forState:UIControlStateNormal];
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidPresent:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内失败回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdFailedToPresent:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    
}

/**
 CXHAdSDKIntertitialAd点击回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClick:(CXHAdSDKInterstitialAd *)interstitialAd loadingPageURL:(nonnull NSString *)loadingPageURL{
    
}

/**
 CXHAdSDKIntertitialAd关闭回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClose:(CXHAdSDKInterstitialAd *)interstitialAd{
    _interstitialAd = nil;
}

/**
 CXHAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposure:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdCloseLandingPage:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}


@end
